package com.heima.controller;

import com.heima.common.ApiResponse;
import com.heima.common.PageResult;
import com.heima.pojo.SkuClass;
import com.heima.pojo.dto.PageDto;
import com.heima.pojo.dto.SkuClassDto;
import com.heima.service.ShopCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/vm-service/skuClass")
public class ShopCategoryController {

    @Autowired
    private ShopCategoryService shopCategoryService;

    /**
     * 新增分类
     */
    @PostMapping()
    public ApiResponse add(@RequestBody SkuClassDto shopDto) {
        return ApiResponse.success(shopCategoryService.save(shopDto));
    }

    /**
     * 分页查询
     */
    @GetMapping("/search")
    public PageResult<SkuClass> search(PageDto pageDto) {
        return shopCategoryService.search(pageDto);
    }

    /**
     * 获取分类基本信息
     * /api/vm-service/skuClass/:classId
     */
    @GetMapping("/{classId}")
    public ApiResponse getById(@PathVariable String classId) {
        return ApiResponse.success(shopCategoryService.getById(classId));
    }

    /**
     * 修改分类
     */
    @PutMapping("/{classId}")
    public Boolean modify(@PathVariable Integer classId, @RequestBody SkuClassDto skuClassDto) {
        skuClassDto.setClassId(classId);
        Boolean isModify = shopCategoryService.modify(skuClassDto);
        return isModify;
    }

    /**
     * 删除商品分类
     */
    @DeleteMapping("/{classId}")
    public void deleteById(@PathVariable String classId) {
        shopCategoryService.deleteById(classId);
    }

}
